//
//  SelectProprieterViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/8.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "SelectProprieterViewController.h"
#import "ProprieterViewController.h"
#import "CityModel.h"
#define Color SetColor(240, 170, 16, 1)
#define BackC SetColor(245, 245, 249, 1)
@interface SelectProprieterViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIButton *oldbtn;
    UILabel *lineLable;
    NSMutableArray *firstCityArr;
    NSMutableArray *secondCityArr;
    BOOL doFirstOpen;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SelectProprieterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BackColor;
    [self initNav:@"选择社长" doBack:YES];
    doFirstOpen = YES;
    firstCityArr = [[NSMutableArray alloc] init];
    secondCityArr = [[NSMutableArray alloc] init];
    
    _tableView.tableFooterView = [[UIView alloc] init];
    
    [self getFirstCityMess];
}
- (void)initView {
    for (int i = 0; i < firstCityArr.count; i ++) {
        
        CityModel *model = firstCityArr[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 50*i, 90, 50);
        btn.titleLabel.font = FontSet(14);
        [btn setTitle:model.name forState:UIControlStateNormal];
        if (i == 0) {
            oldbtn = btn;
            [btn setTitleColor:Color forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
        }else {
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.backgroundColor = BackC;
        }
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(selectCity:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:btn];
    }
    
    lineLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 50)];
    lineLable.backgroundColor = Color;
    [_scrollView addSubview:lineLable];
    
    _scrollView.contentSize = CGSizeMake(90, 50*firstCityArr.count);
}
#pragma mark - 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return secondCityArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Cell = @"cell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:Cell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell];
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    }
    if (secondCityArr.count>0) {
        CityModel *model = secondCityArr[indexPath.row];
        cell.textLabel.text = model.name;
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CityModel *model = secondCityArr[indexPath.row];
    ProprieterViewController *proprieterVC = [[ProprieterViewController alloc] init];
    proprieterVC.titleStr = model.name;
    proprieterVC.cityID = model.cityid;
    [self.navigationController pushViewController:proprieterVC animated:YES];
}
#pragma mark - 按钮点击事件
- (void)selectCity:(UIButton *)btn {
    if (btn != oldbtn) {
        [oldbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        oldbtn.backgroundColor = BackC;
        
        [btn setTitleColor:Color forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor whiteColor];
        
        lineLable.frame = CGRectMake(0, 50*(btn.tag - 100), 2, 50);
        
        oldbtn = btn;
        
        [_tableView reloadData];
    }
    
    CityModel *model = firstCityArr[btn.tag - 100];
    [self getSecondCityByCityID:model.cityid];
}
#pragma mark - 获取数据
- (void)getFirstCityMess {
    NSString *url = [NSString stringWithFormat:GetFirstCity,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSArray *arr = dic[@"citys"];
            for (int i = 0; i < arr.count; i ++) {
                CityModel *model = [RMMapper objectWithClass:[CityModel class] fromDictionary:arr[i]];
                [firstCityArr addObject:model];
            }
            if (doFirstOpen) {
                CityModel *model = firstCityArr[0];
                [self getSecondCityByCityID:model.cityid];
                doFirstOpen = NO;
            }
            [self initView];
        }else {
            [[MessShowView share] showWithMess:dic[@"m"]];
        }
    }];
}
- (void)getSecondCityByCityID:(NSString*)cityID {
    [secondCityArr removeAllObjects];
    NSString *url = [NSString stringWithFormat:GetSecondCity,[NSString stringWithFormat:@"%@",cityID],[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSArray *arr = dic[@"citys"];
            for (int i = 0; i < arr.count; i ++) {
                CityModel *model = [RMMapper objectWithClass:[CityModel class] fromDictionary:arr[i]];
                [secondCityArr addObject:model];
            }
        }else {
            [[MessShowView share] showWithMess:dic[@"m"]];
        }
        [_tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
